﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using nRoute.Components;
using nRoute.Components.Composition;
using nRoute.Components.Messaging;
using nRoute.Navigation.Mapping;
using nRoute.ViewModels;
using nRouteWizard.Wizard.Views;

namespace nRouteWizard.Wizard.ViewModels
{
	[DefineNavigationContent("Content/Pages/NewCustomer/", typeof(NewCustomerView))]
	[MapViewModel(typeof(NewCustomerView))]
	public class NewCustomerViewModel : NavigationViewModelBase
	{
		private const string PAGE_TITLE = "Add Customer Wizard";
		
		private string _firstName;
		private string _lastName;
		private string _address;
		private string _errorMessage;
		private string _currentStep;

		public NewCustomerViewModel()
		{
			base.Title = PAGE_TITLE;
			this.CurrentStep = "Step1";

			SetupCommands();
		}

#region Properties

		public string FirstName
		{
			get { return _firstName; }
			set
			{
				if (!string.Equals(_firstName, value, StringComparison.Ordinal))
				{
					_firstName = value;
					NotifyPropertyChanged(() => FirstName);
				}
			}
		}

		public string LastName
		{
			get { return _lastName; }
			set
			{
				if (!string.Equals(_lastName, value, StringComparison.Ordinal))
				{
					_lastName = value;
					NotifyPropertyChanged(() => LastName);
				}
			}
		}

		public string Address
		{
			get { return _address; }
			set
			{
				if (!string.Equals(_address, value, StringComparison.Ordinal))
				{
					_address = value;
					NotifyPropertyChanged(() => Address);
				}
			}
		}

		public string ErrorMessage
		{
			get { return _errorMessage; }
			set
			{
				if (!string.Equals(_errorMessage, value, StringComparison.Ordinal))
				{
					_errorMessage = value;
					NotifyPropertyChanged(() => ErrorMessage);
				}
			}
		}

		public string CurrentStep
		{
			get { return _currentStep; }
			set
			{
				if (!string.Equals(_currentStep, value, StringComparison.Ordinal))
				{
					_currentStep = value;
					NotifyPropertyChanged(() => CurrentStep);
				}
			}
		}

		public ActionCommand PreviousCommand { get; private set; }

		public ActionCommand NextCommand { get; private set; }

		public ActionCommand FinishCommand { get; private set; }

		public ActionCommand OnCompletedReverseCommand { get; private set; }

#endregion

#region Helpers

		private void SetupCommands()
		{
			PreviousCommand = new ActionCommand(() => 
			{
				CurrentStep = (CurrentStep == "Step2") ? "Step1" : "Step2";
				ErrorMessage = null;
			},
			() => CurrentStep != "Step1")							// only executable when not in step1
			.RequeryOnPropertyChanged(this, () => CurrentStep);		// whenever the CurrentStep property changes, re-query status

			NextCommand = new ActionCommand(() => 
			{
				CurrentStep = (CurrentStep == "Step1") ? "Step2" : "Step3";
				ErrorMessage = null;
			},
			() => CurrentStep != "Step3")							
			.RequeryOnPropertyChanged(this, () => CurrentStep);		

			FinishCommand = new ActionCommand(() =>
			{
				ErrorMessage = null;

				// check for errors
				if (string.IsNullOrWhiteSpace(FirstName))
				{
					ErrorMessage = "First Name is required";
				}
				else if (string.IsNullOrWhiteSpace(Address))
				{
					ErrorMessage = "Address is required";
				}

				// if there no errors
				if (string.IsNullOrWhiteSpace(ErrorMessage))
				{
					// do something with the results
					OnCompletedReverseCommand.Execute();
				}
			});

			OnCompletedReverseCommand = new ActionCommand(() => { });
		}

#endregion

	}
}